package com.bcntouch.touch2act.Contribute;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;
import android.provider.BaseColumns;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;

import com.bcntouch.touch2act.Constants.Constants;
import com.bcntouch.touch2act.Data.DBHelper;

public class ContributionStorage {

	public static final String 	URI_PATH 			= "contribution";
	public static final Uri    	CONTENT_URI 		= Uri.parse(Constants.CONTENT_SCHEME + Constants.AUTHORITY + "/" + URI_PATH);
	
	public static final class ContributionMetaData implements BaseColumns {
			public static final String TABLE_NAME 					= URI_PATH;
			
			public static final String TYPE							= "type";
			public static final String TEXT							= "text";
	}
	
	// These are the Contribution columns that we will retrieve.
    static final String[] CONTRIBUTION_SUMMARY_PROJECTION = new String[] {
    	ContributionMetaData._ID,
    	ContributionMetaData.TYPE,
    	ContributionMetaData.TEXT
    };
	
	public static void initStorage(SQLiteDatabase db) {
		// Create the Products Table
		try {
			db.execSQL( "CREATE TABLE " + ContributionMetaData.TABLE_NAME + " ("
						+ ContributionMetaData._ID 					+ " INTEGER PRIMARY KEY,"
						+ ContributionMetaData.TYPE 				+ " TEXT,"
						+ ContributionMetaData.TEXT       			+ " TEXT"
						+ ");"
					);				
		}
		catch ( Exception e ) {
//			Log.e("ProductsStorage " + " Error creating Database for products" );
		}
	}
	
	public static void insertContribution(Context context, String type, String text) {
		// get the database and run the query
		DBHelper sDB = DBHelper.getHelper(context);
		SQLiteDatabase db = sDB.getWritableDatabase();
		
		ContentValues values = new ContentValues();
		
		values.put(ContributionMetaData.TYPE, type);
		values.put(ContributionMetaData.TEXT, text);
		
		db.insert( ContributionMetaData.TABLE_NAME, ContributionMetaData._ID /* nullColumnHack */, values);
	}
	
	public static Loader<Cursor> getAllContributionsCursorLoader(Context context) {
		Uri baseUri = CONTENT_URI;
        
		String order = ContributionMetaData.TYPE + " ASC";
		
		CursorLoader cursorLoader = new CursorLoader(context, baseUri, CONTRIBUTION_SUMMARY_PROJECTION, null, null, order);
		
        return cursorLoader;
	}
	
}
